package com.yang.flower.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateTime;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yang.flower.common.Constants;
import com.yang.flower.dto.UserDTO;
import com.yang.flower.entity.Menu;
import com.yang.flower.entity.User;
import com.yang.flower.exception.ServiceException;
import com.yang.flower.mapper.RoleMapper;
import com.yang.flower.mapper.RoleMenuMapper;
import com.yang.flower.mapper.UserMapper;
import com.yang.flower.service.MenuService;
import com.yang.flower.service.UserService;
import com.yang.flower.utils.TokenUtils;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description: UserServiceImpl
 * @Author: Yang
 * @DateTime: 2022/11/14 21:48
 **/
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Resource
    private RoleMapper roleMapper;

    @Resource
    private RoleMenuMapper roleMenuMapper;

    @Autowired
    private MenuService menuService;

    //登录
    @Override
    public UserDTO login(UserDTO userDTO) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getUsername,userDTO.getUsername());
        queryWrapper.eq(User::getPassword,userDTO.getPassword());
        User user;
        try{
            user = getOne(queryWrapper);
        } catch (Exception e){
            throw new ServiceException(Constants.CODE_500,"系统错误");
        }
        if(user != null){
            BeanUtil.copyProperties(user,userDTO,true);
            String token = TokenUtils.genToken(user.getId().toString(), user.getPassword());
            userDTO.setToken(token);
            String role = user.getRole();
            List<Menu> roleMenus = getRoleMenu(role);
            userDTO.setMenus(roleMenus);
            return userDTO;
        }else{
            throw new ServiceException(Constants.CODE_600,"用户名或密码错误");
        }
    }

    //注册
    @Override
    public boolean register(User user) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getUsername,user.getUsername());
        User one;
        try{
            one = getOne(queryWrapper);
        }catch (Exception e){
            throw new ServiceException(Constants.CODE_500,"系统错误");
        }
        if(one == null){
//            user.setCreateTime(LocalDateTime.now());
            save(user);
        }
        return true;
    }

    //获取当前角色的菜单列表
    private List<Menu> getRoleMenu(String roleFlag){
        Integer roleId = roleMapper.selectByFlag(roleFlag);
        List<Integer> menuIds = roleMenuMapper.selectByRoleId(roleId);

        //查出系统所有的菜单
        List<Menu> menus = menuService.findMenus("");
        List<Menu> roleMenus = new ArrayList<>();

        //筛选当前用户角色的菜单
        for (Menu menu : menus) {
            if(menuIds.contains(menu.getId())){
                roleMenus.add(menu);
            }
            List<Menu> children = menu.getChildren();
            boolean isSuccess = children.removeIf(child -> !menuIds.contains(child.getId()));
            if (children.size() != 0 && isSuccess){
                roleMenus.add(menu);
            }
        }
        return roleMenus;
    }
}
